package com.itinsys.enma.dao.impl;

import java.util.List;

import net.sf.ehcache.ObjectExistsException;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;

import com.itinsys.enma.dao.DBTemplate;
import com.itinsys.enma.dao.PriorityEnvironmentDAO;
import com.itinsys.enma.dao.hibernate.DBObject;
import com.itinsys.enma.domain.PriorityEnvironment;

public class PriorityEnvironmentDAOImpl implements PriorityEnvironmentDAO {

	private static Logger logger=Logger.getLogger("dao");
	
	public boolean addPriorityEnvironment(PriorityEnvironment priorityEnvironment) {
		return DBObject.addObject(priorityEnvironment);
	}

	public boolean updatePriorityEnvironment(PriorityEnvironment priorityEnvironment) {
		return DBObject.updateObject(priorityEnvironment);
	}

	public boolean deletePriorityEnvironment(PriorityEnvironment priorityEnvironment) {
		return DBObject.removeObject(priorityEnvironment);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PriorityEnvironment> getAllPriorityEnvironment() {
		List<PriorityEnvironment> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from p in class com.itinsys.enma.domain.PriorityEnvironment");
			result=q.list();
		}catch(HibernateException e){
			logger.error("Error al buscar prioridad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PriorityEnvironment getById(int environmentId, int shiftId, String schoolId) {
		List<PriorityEnvironment> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from p in class com.itinsys.enma.domain.PriorityEnvironment where p.environment.id=? and p.shift.id=? and p.school.id=?");
			q.setParameters(new Object[]{environmentId,shiftId,schoolId}, new Type[]{Hibernate.INTEGER,Hibernate.INTEGER,Hibernate.STRING});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("La prioridad ingresada no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar prioridad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public PriorityEnvironment getByEnvironmentIdAndShiftId(int environmentId, int shiftId) {
		List<PriorityEnvironment> result=null;
		Session sess=null;
		try{
			sess=DBTemplate.getConnTemplate().getSessionFactory().openSession();
			Query q=sess.createQuery("from p in class com.itinsys.enma.domain.PriorityEnvironment where p.environment.id=? and p.shift.id=?");
			q.setParameters(new Object[]{environmentId,shiftId}, new Type[]{Hibernate.INTEGER,Hibernate.INTEGER});
			result=q.list();
			if(result.isEmpty()){
				throw new ObjectExistsException("La prioridad ingresada no existe");	
			}
		}catch(HibernateException e){
			logger.error("Error al buscar prioridad por identificador. " ,e);
		}finally {
            if (sess != null){
            	sess.close();	
            }
        }
		return result.get(0);
	}
}
